package com.health.system.process.receiver;

import com.health.system.model.constant.DelayDeleteQueueConstants;
import com.health.system.process.util.QiNiuUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.io.IOException;

/** 删除过期的导出文件
 *
 */
@Component
public class DelayTaskReceiver {
    public static final Logger logger = LoggerFactory.getLogger(DelayTaskReceiver.class);

    @Autowired
    StringRedisTemplate redisTemplate;

    @RabbitListener(queues = DelayDeleteQueueConstants.DELAY_DELETE_QUEUE_NAME)
    public void handler(Message message, Channel channel) throws IOException {
        String taskId = String.valueOf(message.getPayload());
        logger.debug("delete expired task:{}",taskId);
        QiNiuUtil.deleteFile(taskId);
        redisTemplate.opsForHash().delete("export_task",taskId);
    }
}
